package com.itheima.bankaccountsystem;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

/**
 * 创建一个代表银行帐户的BankAccount类。
 * 它应该具有用于帐号、帐户持有人名称、余额的私有实例变量，以及用于帐户总数的静态变量。
 * 实现存入、提取和显示账户信息的方法。
 * 使用继承来创建SavingsAccount和CurrentAccount这样的子类。
 */

public class BankAccount {
    private String idCode;
    private String passWord;
    private String idName;
    private double balance;
    static int count;

    //-------
    public BankAccount() {
        count++;
    }

    public BankAccount(String idCode, String idName, double balance) {
        this.idCode = idCode;
        this.idName = idName;
        this.balance = balance;

    }
    //-----------

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public String getIdCode() {
        return idCode;
    }

    public void setIdCode(String idCode) {
        this.idCode = idCode;
    }

    public String getIdName() {
        return idName;
    }

    public void setIdName(String idName) {
        this.idName = idName;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }
    //---------------

    @Override
    public String toString() {
        return "BankAccount{" +
                "账号='" + idCode + '\'' +
                ", 账户持有人='" + idName + '\'' +
                ", 余额=" + balance +
                ",创建账户的数量=" + count +

                "个}";
    }

    //存入
    static ArrayList<BankAccount> bankAccountList = new ArrayList<>();

    //登录
    public static void getCount(ArrayList<BankAccount> list) {
        Scanner scanner = new Scanner(System.in);
        if (bankAccountList == null) {
            System.out.println("当前系统没有账户,请先创建账户");
            //     BankAccount.setBankAccount();
            return;
        } else {
            System.out.println("请输入卡号");
            String id = scanner.next();
            if (list.get(judge(list, id)).getIdCode().equals(id) == true) {
                System.out.println("请输入密码");
                if (list.get(judge(list, id)).getPassWord().equals(scanner.next()) == true) {
                    System.out.println("登录成功");
                    //---------------
                    while (true) {
                        System.out.println("1、存钱；2、取钱，3、查看账户信息;4、退出");
                        System.out.println("请输入功能编码");
                        int num = scanner.nextInt();
                        switch (num) {
                            case 1:
                                BankAccount.setBankBalance(list, list.get(judge(list, id)).getIdCode());
                                break;
                            case 2:
                                BankAccount.getBankBalance(list, list.get(judge(list, id)).getIdCode());
                                break;
                            case 3:
                                BankAccount.printBankAccount(list, list.get(judge(list, id)).getIdCode());
                                break;
                                case 4:
                                return;
                            default:
                                System.out.println("输入的功能码有误");
                                break;
                        }
                    }


                    // ------------------
                } else {
                    System.out.println("密码错误");
                }

            } else {
                System.out.println("账号不存在，请重新确认");
            }

        }
    }

    //创建账户
    public static ArrayList<BankAccount> setBankAccount(ArrayList<BankAccount> aList) {
        // ArrayList<BankAccount> list = new ArrayList<>();


        BankAccount bankAccount = new BankAccount();
        //系统生成九位卡号
        Random ra = new Random();
        Scanner s = new Scanner(System.in);
        String nums = "";

        for (int i = 0; i < 9; i++) {
            int num = ra.nextInt(10);
            nums += num;
        }
        bankAccount.setIdCode(nums);
        if (judge(aList, bankAccount.getIdCode()) == -1) {
            System.out.println("卡号为：" + nums.toString());
            System.out.println("请输入姓名");
            bankAccount.setIdName(s.next());
            System.out.println("请输入密码");
            bankAccount.setPassWord(s.next());
            System.out.println("首次开户请存入适量金额");
            bankAccount.setBalance(s.nextDouble());
            System.out.println("创建账户成功");
            aList.add(bankAccount);
            System.out.println("开户成功");
            // return aList;
        }
        return aList;
    }


    //存钱
    public static void setBankBalance(ArrayList<BankAccount> list, String idCode) {
        if (judge(list, idCode) != -1) {
            System.out.println("请存入金额");
            Scanner s = new Scanner(System.in);
            list.get(judge(list, idCode)).setBalance(list.get(judge(list, idCode)).getBalance() + s.nextDouble());
            System.out.println("存款成功");
        }
    }

    //取钱
    public static void getBankBalance(ArrayList<BankAccount> list, String idCode) {
        Scanner s = new Scanner(System.in);

        if (judge(list, idCode) != -1) {
            if (list.get(judge(list, idCode)).getBalance() == 0) {
                System.out.println("当前账户余额为0");
            } else {
                System.out.println("请输入取款金额");
                double num = s.nextDouble();

                if (list.get(judge(list, idCode)).getBalance() < num) {
                    System.out.println("取款金额大于账户余额，请重新确认取款金额");
                } else {
                    list.get(judge(list, idCode)).setBalance(list.get(judge(list, idCode)).getBalance() - num);
                    System.out.println("取款成功");
                }

            }

        }
    }


    //显示账户信息
    public static void printBankAccount(ArrayList<BankAccount> list, String idCode) {
        if (judge(list, idCode) != -1) {
            System.out.println(list.get(judge(list, idCode)).toString());
        } else {
            System.out.println("未查询到该账户，请重新确认");
        }

    }

    //判断卡号是否重复
    public static int judge(ArrayList<BankAccount> list, String id) {
        if (id.length() != 9) {
            return -1;
        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).idCode.equals(id)) {
                return i;
            }
        }

        return -1;
    }

}
